package com.example.finalexamcovid_19situations;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class jiangYongKe extends AppCompatActivity {
    String [][] data;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Button button6;
    Button button7;
    Button button8;
    Button button9;
    Button button10;
    Button button11;
    Button button12;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jiang_yong_ke);

        data=queryCitySQL("china.db","city");
        button1=(Button) findViewById(R.id.button);
        button2=(Button) findViewById(R.id.button2);
        button3=(Button) findViewById(R.id.button3);
        button4=(Button) findViewById(R.id.button4);
        button5=(Button) findViewById(R.id.button5);
        button6=(Button) findViewById(R.id.button6);
        button7=(Button) findViewById(R.id.button7);
        button8=(Button) findViewById(R.id.button8);
        button9=(Button) findViewById(R.id.button9);
        button10=(Button) findViewById(R.id.button10);
        button11=(Button) findViewById(R.id.button11);
        button12=(Button) findViewById(R.id.button12);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(0);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(1);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(2);
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(3);
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(4);
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(5);
            }
        });
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(6);
            }
        });
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(7);
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(8);
            }
        });
        button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(9);
            }
        });
        button11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getToast(10);
            }
        });
        button12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(jiangYongKe.this, "敬请期待！", Toast.LENGTH_SHORT).show();
            }
        });
    }


    //查询数据
    String[][] queryCitySQL(String name,String tableName) {
        try {
            SQLiteDatabase sqLiteDatabase = openOrCreateDatabase(name, Context.MODE_PRIVATE,null);
            //查询获得游标
            Cursor cursor = sqLiteDatabase.query(tableName, null,
                    null, null, null, null, null);
            String[][] data = new String[cursor.getCount()][2];
            //判断游标是否为空
            if (cursor.moveToFirst()) {
                //遍历游标
                for (int i = 0; i < cursor.getCount(); i++) {
                    cursor.moveToPosition(i);
                    //获得数据
                    data[i][0] = cursor.getString(0);
                    data[i][1] = cursor.getString(1);
                }
            }
        }catch (RuntimeException runtimeException){
            Toast.makeText(this, "未发现数据，请联网后重试", Toast.LENGTH_SHORT).show();
        }
        return data;
    }

    //获取城市数据
    void getToast(int i){
        try {
            String text = "当前城市感染总人数：" + data[i][0] + "\n当前城市新增人数" + data[i][1];
            Toast.makeText(jiangYongKe.this, text, Toast.LENGTH_SHORT).show();
        }catch (RuntimeException runtimeException){
            Toast.makeText(this, "未发现数据，请联网后重试", Toast.LENGTH_SHORT).show();
        }
    }
}